package com.lyb.jedis;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisTest02 {

    Jedis jedis;

    @BeforeEach
    public void before(){
        jedis = new Jedis("123.56.103.188", 6379);
    }

    @Test
    public void testString() throws InterruptedException {
        String ping = jedis.ping();
        System.out.println("ping = " + ping);
        jedis.set("name", "金毛狮王");
        String name = jedis.get("name");
        System.out.println("name = " + name);
        jedis.setnx("age", "19");
        String age = jedis.get("age");
        System.out.println("age = " + age);

        jedis.setex("code", 50, "1");
        //测试需要睡几秒
        Thread.sleep(5000);
        Long ttl = jedis.ttl("code");
        System.out.println("ttl = " + ttl);
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            System.out.println("key = " + key);
        }
    }

    @Test
    public void testList(){
//        jedis.lpush("amins","狮子","老虎","猴子","熊猫");
//        String amins1 = jedis.rpop("amins");
//        System.out.println("放出了一只 " + amins1);
//        String amins2 = jedis.lpop("amins");
//        System.out.println("又放出了一只 " + amins2);
        List<String> amins = jedis.lrange("amins", 0, -1);
        System.out.println("amins = " + amins);

//        jedis.lpush("amins2","斑马");
//        String rpoplpush = jedis.rpoplpush("amins", "amins2");
//        System.out.println("rpoplpush = " + rpoplpush);
//        List<String> amins21 = jedis.lrange("amins2", 0, -1);
//        System.out.println("amins21 = " + amins21);

    }

    @Test
    public void testSet(){
        jedis.sadd("hs","aa","bbb","cc","dd");
        jedis.sadd("hhs","aaa","bbb","cc","dcd");

        Set<String> hs = jedis.smembers("hs");
        for (String h : hs) {
            System.out.println("h = " + h);
        }
        Boolean sismember = jedis.sismember("hs", "dd");
        System.out.println("sismember = " + sismember);
        Long hs1 = jedis.scard("hs");
        System.out.println("hs1 = " + hs1);

        jedis.smove("hs", "hhs", "dd");
        Set<String> hhs = jedis.smembers("hhs");
        for (String hh : hhs) {
            System.out.println("hh = " + hh);
        }

        Set<String> sinter = jedis.sinter("hs", "hhs");
        System.out.println("sinter = " + sinter);
        Set<String> sunion = jedis.sunion("hs", "hhs");
        System.out.println("sunion = " + sunion);
        Set<String> sdiff = jedis.sdiff("hs", "hhs");
        System.out.println("sdiff = " + sdiff);

    }

    @Test
    public void testHash(){
        Map<String, String> map = new HashMap<>();
        map.put("name","大黄");
        map.put("age","12");
        map.put("address","德国");
        jedis.hmset("dog",map);
        List<String> hmget = jedis.hmget("dog", "name", "age", "address");
        System.out.println("hmget = " + hmget);
        Set<String> dogKey = jedis.hkeys("dog");
        System.out.println("dogKey = " + dogKey);
        List<String> dogVal = jedis.hvals("dog");
        System.out.println("dogVal = " + dogVal);

        jedis.hincrBy("dog","age",-3);
        System.out.println(jedis.hget("dog", "age"));
    }

    @Test
    public void testZset(){
        Map<String, Double> map = new HashMap();
        map.put("茅台",1d);
        map.put("五粮液", 2d);
        map.put("剑南春", 3d);
        jedis.zadd("zs",map);

        Set<String> zs = jedis.zrange("zs", 0, -1);
        for (String z : zs) {
            System.out.println("z = " + z);
        }
        Set<Tuple> zs1 = jedis.zrevrangeWithScores("zs", 0, -1);
        for (Tuple tuple : zs1) {
            String element = tuple.getElement();
            double score = tuple.getScore();
            System.out.println(element + ":" + score);
        }
        Long zrank = jedis.zrank("zs", "五粮液");
        System.out.println("zrank = " + zrank);
    }

    @AfterEach
    public void after(){
        jedis.close();
    }
}
